package com.ruoyi.message.manage.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.plus.web.controller.BasePlusController;
import com.ruoyi.common.core.plus.web.page.PageDto;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.message.general.entity.MessageCountry;
import com.ruoyi.message.general.entity.MessageCountryTemplate;
import com.ruoyi.message.manage.model.dto.ManageMessageCountryDto;
import com.ruoyi.message.manage.model.vo.ManageMessageCountryVo;
import com.ruoyi.message.manage.service.ManageMessageCountryService;
import com.ruoyi.message.manage.service.ManageMessageCountryTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/message/messageCountry")
@Api(tags = "消息国家")
public class ManageMessageCountryController extends BasePlusController {

    @Autowired
    private ManageMessageCountryService manageMessageCountryService;
    @Autowired
    private ManageMessageCountryTemplateService manageMessageCountryTemplateService;

    /**
     * 查询消息国家列表
     */
    @PreAuthorize("@ss.hasPermi('message:message_country:list')")
    @GetMapping("/list")
    @ApiOperation(value = "分页")
    public TableDataInfo<ManageMessageCountryVo.CommonVo> list(ManageMessageCountryDto.CommonDto commonDto) {
        PageDto pageDto = startPage();
        Page<MessageCountry> page = new Page(pageDto.getCurrentPage(), pageDto.getPageSize());
        if (pageDto.getOrderItem() != null) {
            page.addOrder(pageDto.getOrderItem());
        }
        MessageCountry messageCountry = Convert.convert(MessageCountry.class, commonDto);
        LambdaQueryWrapper<MessageCountry> messageCountryLambdaQueryWrapper = Wrappers.lambdaQuery(MessageCountry.class)
                .like(messageCountry.getName() != null, MessageCountry::getName, messageCountry.getName())
                .like(messageCountry.getAreaCode() != null, MessageCountry::getAreaCode, messageCountry.getAreaCode());
        page = manageMessageCountryService.getByPage(page, messageCountryLambdaQueryWrapper);
        return getDataTable(page);
    }

    /**
     * 查询消息国家列表
     */
    @PreAuthorize("@ss.hasPermi('message:message_country:list')")
    @GetMapping("/listAll")
    @ApiOperation(value = "分页")
    public AjaxResult listAll() {
        List<MessageCountry> messageCountryList = manageMessageCountryService.getByList(null);
        List<ManageMessageCountryVo.CommonVo> list = Convert.convert(new TypeReference<List<ManageMessageCountryVo.CommonVo>>() {
        }, messageCountryList);
        return AjaxResult.success(list);
    }

    /**
     * 导出消息国家列表
     */
    @PreAuthorize("@ss.hasPermi('message:message_country:export')")
    //@Log(title = "消息国家", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出")
    public void export(HttpServletResponse response, ManageMessageCountryDto.CommonDto commonDto) throws IOException {
        MessageCountry messageCountry = Convert.convert(MessageCountry.class, commonDto);
        List<MessageCountry> list = manageMessageCountryService.getByList(messageCountry);
        ExcelUtil<MessageCountry> util = new ExcelUtil<MessageCountry>(MessageCountry.class);
        util.exportExcel(response, list, "messageCountry");
    }

    /**
     * 获取消息国家详细信息
     */
    @PreAuthorize("@ss.hasPermi('message:message_country:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "查看")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        ManageMessageCountryVo.CommonVo commonVo = Convert.convert(ManageMessageCountryVo.CommonVo.class, manageMessageCountryService.getById(id));
        return AjaxResult.success(commonVo);
    }

    /**
     * 新增消息国家
     */
    @PreAuthorize("@ss.hasPermi('message:message_country:add')")
    //@Log(title = "消息国家", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增")
    public R add(@RequestBody ManageMessageCountryDto.CommonDto commonDto) {
        MessageCountry messageCountry = Convert.convert(MessageCountry.class, commonDto);
        manageMessageCountryService.save(messageCountry);
        return toAjax(1);
    }

    /**
     * 修改消息国家
     */
    @PreAuthorize("@ss.hasPermi('message:message_country:edit')")
    //@Log(title = "消息国家", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改")
    public R edit(@RequestBody ManageMessageCountryDto.CommonDto commonDto) {
        MessageCountry messageCountry = Convert.convert(MessageCountry.class, commonDto);
        return toAjax(manageMessageCountryService.update(messageCountry));
    }

    /**
     * 删除消息国家
     */
    @PreAuthorize("@ss.hasPermi('message:message_country:remove')")
    //@Log(title = "消息国家", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除")
    public R remove(@PathVariable Long[] ids) {
        LambdaQueryWrapper<MessageCountry> messageCountryLambdaQueryWrapper = Wrappers.lambdaQuery(MessageCountry.class).in(MessageCountry::getId, ids);
        manageMessageCountryService.delete(messageCountryLambdaQueryWrapper);
        LambdaQueryWrapper<MessageCountryTemplate> messageCountryTemplateLambdaQueryWrapper = Wrappers.lambdaQuery(MessageCountryTemplate.class).in(MessageCountryTemplate::getCountryId, ids);
        manageMessageCountryTemplateService.delete(messageCountryTemplateLambdaQueryWrapper);
        return toAjax(1);
    }

}
